package com.dragon.flow.juel;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @title: 自定义的解析器
 * @author: bruce.liu
 * @since: 2023/4/26 23:51
 */
@Component("cuel")
public class CustomJuel {

    /**
     * 多个参数组装成list参数
     *
     * @param datas 可变参数
     *              使用方式：如：${cuel.toList('10000','10001','10002')}
     * @return
     */
    public List<String> toList(String... datas) {
        Set<String> dataSet = new HashSet<>();
        if (datas.length > 0) {
            for (String data : datas) {
                dataSet.add(data);
            }
        }
        return dataSet.stream().collect(Collectors.toList());
    }

    /**
     * 传入的日期与当前日期比较相等
     *
     * @param date 传入的日期
     *             使用方式：${cuel.deq(date)}
     * @return
     */
    public boolean deq(String date) {
        if (StringUtils.isNotBlank(date)) {
            String currentDate = DateUtil.format(new Date(), "yyyy-MM-dd");
            if (currentDate.equals(date)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 传入的日期与当前日期比较小于
     *
     * @param date 传入的日期
     *             使用方式：${cuel.dlt(date, 2)}
     * @return
     */
    public boolean dlt(String date, Integer num) {
        if (StringUtils.isNotBlank(date)) {
            LocalDateTime localDateTime = DateUtil.parseLocalDateTime(date, "yyyy-MM-dd");
            Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
            Date currentDate = Date.from(instant);
            long between = DateUtil.between(currentDate, new Date(), DateUnit.DAY);
            if (between < num) {
                return true;
            }
        }
        return false;
    }

    public boolean dlte(String date, Integer num) {
        if (StringUtils.isNotBlank(date)) {
            LocalDateTime localDateTime = DateUtil.parseLocalDateTime(date, "yyyy-MM-dd");
            Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
            Date currentDate = Date.from(instant);
            long between = DateUtil.between(currentDate, new Date(), DateUnit.DAY);
            if (between <= num) {
                return true;
            }
        }
        return false;
    }
    /**
     * 传入的日期与当前日期比较大于
     *
     * @param date 传入的日期
     *             使用方式：${cuel.dgt(date, 2)}
     * @return
     */
    public boolean dgt(String date, Integer num) {
        if (StringUtils.isNotBlank(date)) {
            LocalDateTime localDateTime = DateUtil.parseLocalDateTime(date, "yyyy-MM-dd");
            Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
            Date currentDate = Date.from(instant);
            long between = DateUtil.between(currentDate, new Date(), DateUnit.DAY);
            if (between > num) {
                return true;
            }
        }
        return false;
    }

    /**
     * 传入的日期与当前日期比较大于
     *
     * @param date 传入的日期
     *             使用方式：${cuel.dgte(date, 2)}
     * @return
     */
    public boolean dgte(String date, Integer num) {
        if (StringUtils.isNotBlank(date)) {
            LocalDateTime localDateTime = DateUtil.parseLocalDateTime(date, "yyyy-MM-dd");
            Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
            Date currentDate = Date.from(instant);
            long between = DateUtil.between(currentDate, new Date(), DateUnit.DAY);
            if (between >= num) {
                return true;
            }
        }
        return false;
    }

}
